package com.cos.shop.services.impl;

import com.cos.shop.common.ResponseResult;
import com.cos.shop.dao.ProductDao;
import com.cos.shop.dao.impl.ProductDaoImpl;
import com.cos.shop.entity.Product;
import com.cos.shop.services.ProductServices;
import com.cos.shop.util.PageHelper;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: COS
 * @time: 2023/5/11 10:30
 * @description:
 */
public class ProductServicesImpl implements ProductServices {
    private ProductDao pd = new ProductDaoImpl();

    @Override
    public ResponseResult getGoodsById(int id) {
        try {
            if (id>0) {
                return ResponseResult.createSuccess(pd.getGoodsById(id));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("没有查询到数据");
    }

    @Override
    public ResponseResult getProByTypeId(int typeId, int pageNum) {
        try {
            //1 查询数九总条数
            int count = pd.getProCount(typeId).intValue();
            //2 创建pageHelper
            PageHelper ph = new PageHelper(pageNum, 8, count);
            //3 访问dao 获取分页数据
            List<Product> proList = pd.getProByPageWithTypeId(typeId, ph.getRowStart(), ph.getPageSize());
            //4 封装数据
            ph.setList(proList);
            return ResponseResult.createSuccess(ph);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("没啦");
    }
}
